package com.EventHandler;


import android.content.Context;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.Dao.CommentDao;
import com.Dao.UserDao;
import com.example.myapplication.ArticleActivity;
import com.example.myapplication.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 负责加载和管理评论。
 */
public class CommentManager {
    private Context context;
    private CommentDao commentDao;
    private UserDao userDao;
    private int newsId;
    private int userId;

    private EditText nav_commit_edit;
    private LinearLayout comments_section;

    public CommentManager(Context context, int newsId, int userId) {
        this.context = context;
        this.newsId = newsId;
        this.userId = userId;
        commentDao = new CommentDao(context);
        userDao = new UserDao(context);
    }

    public void initializeUI() {
        nav_commit_edit = ((ArticleActivity) context).findViewById(R.id.nav_commit_edit);
        comments_section = ((ArticleActivity) context).findViewById(R.id.comments_section);
    }

    private String formatDate(long time) {
        Date date = new Date(time ); // 假设时间戳是秒级单位，需要转换为毫秒级
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(date);
    }

    public void loadComments() {

    }

    public void addComment(int userId, String comment, long time) {
// 清空评论区
//        comments_section.removeAllViews();
//
//        // 获取所有评论
//        List<Comment> comments = commentDao.getCommentsByNewsId(newsId);
//
//        // 动态添加评论到UI
//        for (Comment c : comments) {
//            View commentView = ((ArticleActivity) context).getLayoutInflater().inflate(R.layout.comment_item, null);
//
//            TextView commentUser = commentView.findViewById(R.id.comment_user);
//            TextView commentText = commentView.findViewById(R.id.comment_text);
//            TextView commentTime = commentView.findViewById(R.id.comment_time);
//
//            User user = userDao.getUserById(c.getUserId());
//            if (user != null) {
//                commentUser.setText(user.getName());
//            } else {
//                commentUser.setText("未知用户");
//            }
//
//            commentText.setText(c.getContent());
//            commentTime.setText(formatDate(c.getCreatedAt()));
//            comments_section.addView(commentView);
//
//        }
    }

    public void close() {
        commentDao.close();
    }

}
